<?php
$appkey = "your appkey";
$appsecret = "your appsecret";
$api_url = "http://127.0.0.1:8080/v1";

function makeSign($method, $params = [])
{
    $params['appkey']    = $GLOBALS['appkey'];
    $params['appsecret'] = $GLOBALS['appsecret'];
    $params['method']    = $method;

    ksort($params);
    return sha1(strtolower(http_build_query($params)));
}

function http_query($url, $method, $sign, $data)
{
    $headers = [
        'llt-appKey:' . $GLOBALS['appkey'], 
        'llt-sign:' . $sign,
    ];

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

    if ($data) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));        
    }

    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $output = curl_exec($curl);
    
    curl_close($curl);
    return $output;    
}

//激活设备
function active($device)
{
    $method = "PUT";
    $params = [
        'nostr' => microtime(true),
    ];

    $url = $GLOBALS['api_url'] . "/device/{$device}/active?" . http_build_query($params) ;
    $sign = makeSign($method, $params);

    return http_query($url, $method, $sign, null);
}

//设备详情
function detail($device)
{
    $method = "GET";
    $params = [
        'nostr' => microtime(true),
    ];

    $url = $GLOBALS['api_url'] . "/device/{$device}?" . http_build_query($params);
    $sign = makeSign($method, $params);

    return http_query($url, $method, $sign, null);
}

//订单
function order($device, $channel = 1, $timeout = 15, $src = [])
{
    $orderNO = "NO".microtime(true);
    $url = $GLOBALS['api_url'] . "/order/{$orderNO}";
    $method = "POST";
    $params = [
        "deviceGUID" =>  $device,
        "channel" => $channel,
        "timeout" => $timeout,
        "src" => $src, 
        "nostr" => microtime(true),
    ];

    $sign = makeSign($method, $params);    
    return http_query($url, $method, $sign,  $params);
}

$device = 'device00001';
var_dump(active($device));
var_dump(detail($device));
var_dump(order($device));